package org.flexr.service
{
	import flash.desktop.NativeApplication;
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.NativeProcessExitEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	
	import mx.controls.Alert;
	
	import org.flexr.events.ServiceEvent;
	import org.flexr.model.Config;
	import org.flexr.umvc.CentralDispatcher;
	
	public class TailService extends NativeProcess
	{
		
		private var nativeProcessStartupInfo:NativeProcessStartupInfo
		private var _logfile:String = '';
		private var processArgs:Vector.<String>;
		private var file:File;
		private var configXML:XML;
		private var dispatcher:CentralDispatcher;
		
		public function TailService()
		{
			super();
			dispatcher = CentralDispatcher.instance;
			 

			NativeApplication.nativeApplication.addEventListener(Event.EXITING, onAppExit);

			configXML = Config.instance.config;	
			nativeProcessStartupInfo = new NativeProcessStartupInfo();
    		file = File.applicationDirectory.resolvePath("tail.exe");
     		 
      		nativeProcessStartupInfo.executable = file;
    		
    		processArgs = new Vector.<String>(2);
     
    		processArgs[0] = "-f";
    		processArgs[1] = _logfile;
    		
    		logFile = 'wef';

    		nativeProcessStartupInfo.arguments = processArgs;
 		}
		private function onAppExit(e:Event):void
		{
 			tailStop()
		}
		private function onOutputData(e:ProgressEvent):void
		{
 			dispatcher.dispatchEvent(new ServiceEvent(ServiceEvent.TRACE_DATA, standardOutput.readUTFBytes(standardOutput.bytesAvailable)));
		}
		public function tailStart():void
		{
			if(processArgs[1] != '' && this.running != true )
			{
        		if( new File(_logfile).exists)
        		{
	        		this.start(nativeProcessStartupInfo);
				 	addEventListener(NativeProcessExitEvent.EXIT, onExit);
				 	if(!hasEventListener(ProgressEvent.STANDARD_OUTPUT_DATA))
				 	addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA,onOutputData);
         		}
        		else 
        		{
        			Alert.show('Log file doesn\'t exist');
        		}
			}
		}
		private function onExit(e:Event):void
		{
			this.start(nativeProcessStartupInfo);
 		}
		public function tailStop():void
		{
			this.removeEventListener(NativeProcessExitEvent.EXIT, onExit);
			this.exit(true);
			
		}
		public function set logFile(f:String):void
		{
			if(configXML.logfile.@path == '')
			{
				_logfile = f;
				_logfile = (File.applicationStorageDirectory as File).nativePath;
		 		_logfile = _logfile.slice(0,_logfile.indexOf('FlexrTracer'));
				_logfile += '\Macromedia\\Flash Player\\Logs\\flashlog.txt';
				configXML.logfile.@path = _logfile;				
			}
			else
			{
				_logfile = configXML.logfile.@path; 
			}
	 		processArgs[1] = _logfile;
		}
		public function get logFile():String
		{
			return _logfile;
		}
		
	}
}